// ActionScript file
import com.dynamicflash.util.Base64;
import com.tinkerlog.WebCam;

import component.LoadingProgress;

import flash.display.Bitmap;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.utils.Timer;

import mx.core.UIComponent;
import mx.graphics.codec.JPEGEncoder;
import mx.managers.PopUpManager;

import org.flexlancer.Logger.Logger;
import org.flexlancer.controller.Picture;
import org.flexlancer.events.PictureDoubleClickEvent;
			
			private var webCam:WebCam;
			[Bindable]
			private var picIndex:Number = 1;
			private var lpWaitForServer:LoadingProgress  = null ;
			//////shahid code
			///////
			public var LargPicture:Picture = null;
		
			
			////////////
			private function init():void {
				try{
				     // webCam = new WebCam(640, 480);
				     webCam = new WebCam(640,480);
				     
				      var ref:UIComponent = new UIComponent();
				      myPreview.removeAllChildren();
				      myPreview.addChild(ref);
				      ref.addChild(webCam);
				      this.gallary.addEventListener(PictureDoubleClickEvent.PICTURE_DOUBLE_CLICK,OnPictureClicked);
				}catch(err:Error){
					Logger.LogE(err);
				}
		      
		    }
		    
		    private function takeSnapshot():void {
				try{
			        var photoData:Bitmap = this.webCam.getSnapshot();
			        photoData.width = 160;
			        photoData.height = 120;
			        var insertPic:Picture = new Picture(photoData,this.txt_namingConvention.text + this.picIndex.toString());
			        insertPic.width = 160;
			        insertPic.height = 120;
			        this.gallary.AddPicture(insertPic);
			        this.picIndex++;
				}catch(err:Error){
					Logger.LogE(err);
				}
		    }
		    
		    private function takeBurst():void {
		    	try{
		    		var timer:Timer = new Timer(1000,this.ns_burstValue.value-1);
		    		timer.addEventListener(TimerEvent.TIMER,this.TimerCalled);
		    		timer.start();
		    		this.takeSnapshot();
		    	}catch(err:Error){
		    		throw err;
		    	}
		    }
		    
		    private function TimerCalled(evt:TimerEvent):void{
		    	try{
		    		this.takeSnapshot();
		    	}catch(err:Error){
		    		Logger.LogE(err);
		    	}
		    }
			
			public function OnDeleteSelected():void{
				try{
					
					this.gallary.RemoveSelected();
					////shaihd code
					
				//	 this.obj1.caller = this;
					
					////code ends
					
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			//==========================================================
      private function uploadSnapshot(parmPicture:Picture):void {
		if ( this.lpWaitForServer == null ){
			this.lpWaitForServer = PopUpManager.createPopUp(this,LoadingProgress,true) as LoadingProgress;
			PopUpManager.centerPopUp(this.lpWaitForServer);
		}

          var uic:UIComponent = new UIComponent();
          uic.addChild(parmPicture.BitMap2);
          
          var bitmap:Bitmap = uic.getChildAt(0) as Bitmap;
          var quality:Number = (this.cmbQuality.selectedItem.data) as Number;
          
          var jpgEncoder:JPEGEncoder = new JPEGEncoder(quality);
          var jpgBytes:ByteArray = jpgEncoder.encode(bitmap.bitmapData);        
          uploadPhoto(jpgBytes,parmPicture);
      }
      private function uploadPhoto(imageData:ByteArray,parmPicture:Picture):void {
        var request:URLRequest = new URLRequest("http://localhost:50336/upload.aspx");
        var vars:URLVariables = new URLVariables();
        vars.name = parmPicture.Name;
        //vars.comment = commentField.text;
        
        vars.bindata = Base64.encodeByteArray(imageData);
        request.method = "POST";       
        var loader:URLLoader = new URLLoader();               
        loader.addEventListener(Event.COMPLETE, uploadPhotoHandler);
        request.data = vars;
        loader.load(request);        
      }
      private function uploadPhotoHandler(event:Event):void {
      	//Alert.show(event.target.data,"Image uploaded");
      	this.upload_count --;
      	if ( this.upload_count == 0 ){
      		PopUpManager.removePopUp(this.lpWaitForServer);
      		this.lpWaitForServer = null;
      	}else{
      		this.uploadSnapshot(this.gallary.Collection[this.upload_count - 1]);
      	}
      }
			
			//==========================================================
			private var upload_count:Number = 0;
			private function OnSaveAll():void{
				try{
					this.upload_count = this.gallary.Collection.length;
					
					/*for each ( var singleImage:Picture in this.gallary.Collection ){
						this.uploadSnapshot(singleImage);
					}*/
					if ( this.upload_count > 0 )
						this.uploadSnapshot(this.gallary.Collection[this.upload_count - 1]);
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			private function OnSaveClick():void{
				try{
					if ( this.chkAll.selected ){
						this.OnSaveAll();						
					}else{
						if ( this.gallary.Selected != null ){
							this.uploadSnapshot(this.gallary.Selected);
							this.upload_count = 1;
						}
					}
				}catch(err:Error){
					Logger.LogE(err);
				}
			}

			public function OnPictureClicked(evt:PictureDoubleClickEvent):void {
				try {
					this.LargPicture= evt.selected_picture;
					
					var LargeImage:ShowLargImage = PopUpManager.createPopUp(this,ShowLargImage,true) as ShowLargImage;
					LargeImage.caller=this;
					LargeImage.displayImage.source = this.LargPicture.BitMap;
					PopUpManager.centerPopUp(LargeImage);
					
					
					
				}catch(err:Error){
					Logger.LogE(err);
				}
			} 